package com.example.administrator.myapplication.register;

import android.app.Dialog;
import android.app.Fragment;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.IdRes;
import android.support.annotation.Nullable;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.NumberPicker;
import android.widget.RadioGroup;
import android.widget.Toast;

import com.example.administrator.myapplication.R;
import com.example.administrator.myapplication.login.LoginActivity;
import com.example.administrator.myapplication.tools.TestData;


import java.util.Timer;
import java.util.TimerTask;


import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.SaveListener;


/**
 * Created by Administrator on 2017/11/7.
 */

public class RegisterFragment extends Fragment{

    private String sex_saver;                    //用来存储用户选择了什么
    private EditText register_username;
    private EditText register_password;
//    private EditText register_mobilePhoneNumber;
    private RadioGroup register_radiogroup;
    private EditText register_pwd_twice;
//    private EditText register_userVerify;     //验证码
    private Button register_bt;
    private Button bt_register_age;
    private Button bt_register_weight;
    private Button bt_register_height;

    private LayoutInflater factory;
    private View myView;
    private int register_height = 0;
    private int register_weight = 0;
    private int register_age = 0;
    private int numSaver;
    private int flag;
    private boolean swich;
    private NumberPicker numberpicker;


  
    


    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        final View view=inflater.inflate(R.layout.fragment_register,container,false);

        init(view);

        sex_saver="0";                //默认为男
        register_radiogroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
                //当选中的是男时
                if(group.getCheckedRadioButtonId()==R.id.rb_register_man){
                    sex_saver="0";         //0代表男
                }else if(group.getCheckedRadioButtonId()==R.id.rb_register_woman){
                    sex_saver="1";
                }
            }
        });

        //设置点击事件
        bt_register_height.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SetNumberpicker(R.id.bt_register_height);
                numberpicker.setValue(170);
            }
        });

        bt_register_weight.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SetNumberpicker(R.id.bt_register_weight);
                numberpicker.setValue(50);
            }
        });

        bt_register_age.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SetNumberpicker(R.id.bt_register_age);
                numberpicker.setValue(18);
            }
        });



        //创建数据库表
       final TestData bu = new TestData();
        register_bt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //用户名应该为
                String username =register_username.getText().toString();
                String reg="\\w{6,16}";
                Boolean flag_user=username.matches(reg);
                if(!flag_user){
                    Toast.makeText(getActivity(),"请正确输入6~16位的用户名",Toast.LENGTH_SHORT).show();
                }else if(!register_password.getText().toString().equals(register_pwd_twice.getText().toString())){
                    Toast.makeText(getActivity(),"密码两次输入不相同",Toast.LENGTH_SHORT).show();
                }else if(register_height==0||register_weight==0||register_age==0){
                    Toast.makeText(getActivity(),"有选项没有选择",Toast.LENGTH_SHORT).show();
                }
                else{
                    bu.setUsername(username);
                    bu.setPassword(register_password.getText().toString());
                    bu.setSex(sex_saver);
                    bu.setHeight(String.valueOf(register_height));
                    bu.setWeight(String.valueOf(register_weight));
                    bu.setAge(String.valueOf(register_age));
//                   bu.setMobilePhoneNumber(register_mobilePhoneNumber.getText().toString());
//                    bu.setVerifynumber(register_userVerify.getText().toString());

                    bu.signUp(new SaveListener<TestData>() {
                        public void done(TestData s, BmobException e) {
                            if(e==null){
                                Toast.makeText(getActivity(),"注册成功",Toast.LENGTH_SHORT).show();
                                //设置一个定时器 ，两秒之后执行以下代码
                                Timer timer =new Timer();
                                final Intent intent =new Intent(getActivity(), LoginActivity.class);
                                timer.schedule(new TimerTask() {
                                    @Override
                                    public void run() {
                                        startActivity(intent);   //两秒之后执行以下代码
                                    }
                                },2000);
                            }else{
                                if(e.getErrorCode()==301){
                                    Toast.makeText(getActivity(),"请输入正确的手机号",Toast.LENGTH_SHORT).show();
                                }else if(e.getErrorCode()==202){
                                    Toast.makeText(getActivity(),"用户名已经存在",Toast.LENGTH_SHORT).show();
                                }else if(e.getErrorCode()==304){
                                    Toast.makeText(getActivity(),"用户名或密码为空",Toast.LENGTH_SHORT).show();
                                }
                                e.printStackTrace();
                                // Toast.makeText(getActivity(),"用户名已存在",Toast.LENGTH_SHORT).show();
                            }
                        }
                    });
                }
            }
        });

        return view;
    }

    public void init(View view){
        register_username=(EditText) view.findViewById(R.id.register_user_et);
        register_password=(EditText) view.findViewById(R.id.register_pwd_et);
//        register_mobilePhoneNumber=(EditText) view.findViewById(R.id.register_phone_et);
//        register_userVerify=(EditText) view.findViewById(R.id.register_verify_et);
        register_radiogroup= (RadioGroup) view.findViewById(R.id.rg_register_sex);
        register_bt=(Button)view.findViewById(R.id.register_bt);
        register_pwd_twice= (EditText) view.findViewById(R.id.register_pwd_twice);

        bt_register_age= (Button) view.findViewById(R.id.bt_register_age);
        bt_register_height= (Button) view.findViewById(R.id.bt_register_height);
        bt_register_weight= (Button) view.findViewById(R.id.bt_register_weight);

    }

    public void SetNumberpicker(int id) {
        String name = null;
        factory = LayoutInflater.from(getActivity());
        if (id == R.id.bt_register_height) {
            name = "设置身高";
            flag = 1;
        } else if (id == R.id.bt_register_weight) {
            name = "设置体重";
            flag = 2;
        }else if(id==R.id.bt_register_age){
            name="设置年龄";
            flag=3;
        }
        initNumberpicker();
        Dialog dialog = new AlertDialog.Builder(getActivity())

                .setTitle(name)
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int a) {
                        if(flag==1&&swich){
                            numSaver=170;
                        }else if(flag==2&&swich){
                            numSaver=50;
                        }else if(flag==3&&swich){
                            numSaver=18;
                        }
                        if (flag == 1) {
                            register_height = numSaver;
                            bt_register_height.setTextColor(Color.parseColor("#ff0000"));
                            bt_register_height.setText(String.valueOf(register_height));
                        } else if (flag == 2) {
                            register_weight = numSaver;
                            bt_register_weight.setTextColor(Color.parseColor("#ff0000"));
                            bt_register_weight.setText(String.valueOf(register_weight));
                        }else if(flag == 3){
                            register_age = numSaver;
                            bt_register_age.setTextColor(Color.parseColor("#ff0000"));
                            bt_register_age.setText(String.valueOf(register_age));
                        }
                    }
                })
                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                }).setView(myView).create();
        dialog.show();
    }

    public void initNumberpicker() {
        myView = factory.inflate(R.layout.activity_numberpicker, null);
        numberpicker = (NumberPicker) myView.findViewById(R.id.test_numberpicker);
        swich=true;
        if (flag==1){
            numberpicker.setMinValue(120);
            numberpicker.setMaxValue(210);
            numberpicker.setWrapSelectorWheel(false);
        }else if(flag==2){
            numberpicker.setMinValue(30);
            numberpicker.setMaxValue(120);
            numberpicker.setWrapSelectorWheel(false);
        }else if(flag==3){
            numberpicker.setMinValue(8);
            numberpicker.setMaxValue(60);
            numberpicker.setWrapSelectorWheel(false);
        }
        numberpicker.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
            @Override
            public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
                numSaver = newVal;
                swich=false;
            }
        });

    }
}
